<template>
  <div class="dashboard-container">
    <div class="app-container">

      <el-card>
        <el-row><el-button class="btn" size="medium" type="primary" @click="back">返回</el-button></el-row>
        <el-tabs>
          <el-tab-pane label="个人详情">
            <user-info :tree-node="userInfo" />
          </el-tab-pane>
          <!-- <el-tab-pane label="设备租借记录">
            <el-table border :data="myequiplist">
              <el-table-column type="index" label="序号" width="100" />
              <el-table-column label="名称" prop="name" />
              <el-table-column label="租借原因" prop="reason" />
              <el-table-column label="租借人" prop="student" />
              <el-table-column label="租借进度">
                <template slot-scope="scope">
                  <el-tag :type="getStatus(scope.row.progress).type">{{ getStatus(scope.row.progress).label }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="租借时间" prop="lendtime" />
              <el-table-column label="归还时间">
                <template slot-scope="scope">
                  <span v-if="scope.row.returntime||scope.row.progress!==1">{{ scope.row.returntime }}</span>
                  <el-tag v-else-if="latedReturn(scope.row) <= 0" type="warning">待归还，余剩{{ -latedReturn(scope.row) }}天</el-tag>
                  <el-tag v-else type="danger">已逾期{{ latedReturn(scope.row) }}天</el-tag>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane> -->
          <el-tab-pane label="角色信息">
            <role-info />
          </el-tab-pane>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="studentname">
                <el-input v-model="userInfo.studentname" style="width:300px" disabled />
              </el-form-item>
              <el-form-item label="新密码:" prop="password">
                <el-input v-model="password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { lendHistoryByStudent } from '@/api/equip'
import { getInfo, editUser } from '@/api/user'
import UserInfo from './user-info'
import RoleInfo from './role-info'

export default {
  components: {
    UserInfo,
    RoleInfo
  },
  data() {
    return {
      myequiplist: [],
      password2: '',
      userId: this.$route.params.id, // 这样可以后面直接通过 this.userId进行获取数据
      userInfo: {
        //   专门存放基本信息
        studentname: '',
        password: ''
      },
      rules: {
        studentname: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async lendHistoryByStudent() {
      const rows = await lendHistoryByStudent(this.userInfo.studentname)
      this.myequiplist = rows
    },
    async getUserDetailById() {
      this.userInfo = await getInfo(this.userId)
      this.lendHistoryByStudent()
    },
    saveUser() {
      this.userInfo.sex = this.userInfo.sex.toString()
      this.userInfo.phone = this.userInfo.phone.toString()
      this.userInfo.password = this.password2
      this.$confirm('确定要更改该用户密码吗').then(() => {
        // 如果点击了确定就会进入then
        return editUser(this.userId, this.userInfo) // 返回promise对象
      }).then(() => {
        this.$message.success('更改成功')
        this.password2 = ''
      })
    },
    back() {
      if (this.$route.path.indexOf('users') > 0) {
        this.$router.push('/users')
      } else {
        this.$router.push('/')
      }
    }
  }
}
</script>

<style>
  .btn {
    display: flow;
    float: right;
  }
</style>
